package cn.stylefeng.guns;

import cn.hutool.core.io.FileUtil;
import cn.hutool.http.HttpUtil;
import cn.stylefeng.guns.modular.common.service.CommonService;
import cn.stylefeng.guns.modular.info.service.AicaiService;
import cn.stylefeng.guns.modular.info.service.Game2Service;
import cn.stylefeng.guns.modular.info.service.GameService;
import cn.stylefeng.guns.wechat.WeChatMsgSend;
import cn.stylefeng.guns.wechat.WeChatUrlData;
import cn.stylefeng.guns.schedule.service.AddLeagueService;
import cn.stylefeng.guns.schedule.service.ServerMService;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.Charset;
import java.text.ParseException;

/**
 * @创建人 xiesm
 * @创建时间 2020/6/7
 * @描述
 */
@Slf4j
@Controller
@RequestMapping("/test")
public class FootController {
    @Autowired
    private ServerMService serverMService;
    @Autowired
    private AddLeagueService addLeagueService;
    @Autowired
    private CommonService commonService;
    @Autowired
    private GameService gameService;
    @Autowired
    private Game2Service game2Service;
    @Autowired
    private AicaiService aicaiService;

    @RequestMapping("/foot")
    @ResponseBody
    public String test(String num) throws IOException, ParseException {
        serverMService.getSummary(num);
        return "ok";
    }

    @RequestMapping("/before")
    @ResponseBody
    public String before(String num) throws IOException, ParseException {
        serverMService.before(num);
        return "ok";
    }

    @RequestMapping("/beforeAll")
    @ResponseBody
    public String beforeAll() {
        serverMService.beforeAll();
        return "ok";
    }

    @RequestMapping("/sendMsg")
    @ResponseBody
    public String sendMsg(String msg, String appid) throws IOException {
        WeChatMsgSend swx = new WeChatMsgSend();
        String postdata = swx.createpostdata("13459446228", "text", Integer.valueOf(appid), "content", msg);
        swx.post("utf-8", WeChatMsgSend.CONTENT_TYPE, (new WeChatUrlData()).getSendMessage_Url(), postdata, commonService.getWechatToken(Integer.valueOf(appid)));
        return "ok";
    }

    @RequestMapping("/token")
    @ResponseBody
    public String token() throws IOException {
        return commonService.getWechatToken(100005);
    }

    @RequestMapping("/select")
    @ResponseBody
    public String select(String num) {
        serverMService.select();
        return "ok";
    }

    @RequestMapping("/addLeagueService")
    @ResponseBody
    public String addLeagueService() throws IOException {
        addLeagueService.addLeague();
        return "ok";
    }

    @RequestMapping("/setScore")
    @ResponseBody
    public String setScore() {
        gameService.setScore();
        return "ok";
    }

    @RequestMapping("/getSeason")
    @ResponseBody
    public String getSeason(HttpServletResponse response, String date, String endDate) {
        gameService.getSeason(response, date, endDate);
        return "ok";
    }

    @RequestMapping("/getDayReport")
    @ResponseBody
    public String getDayReport() throws IOException {
        serverMService.dayReport();
        return "ok";
    }

    @RequestMapping("/getWeekReport")
    @ResponseBody
    public String getWeekReport() throws IOException {
        serverMService.weekReport();
        return "ok";
    }


    @RequestMapping("/getSeason2")
    @ResponseBody
    public String getSeason2(HttpServletResponse response, String date, String endDate) {
        game2Service.getSeason(response, date, endDate, "1");
        return "ok";
    }


    @RequestMapping("/getSeason3")
    @ResponseBody
    public String getSeason3(HttpServletResponse response, String date, String endDate) {
        game2Service.getSeason(response, date, endDate, "2");
        return "ok";
    }

    @RequestMapping("/saveAicai")
    @ResponseBody
    public String saveAicai(String date) throws IOException {
        aicaiService.searchMatch(date);
        return "ok";
    }


    @RequestMapping("/aicaiOwnGoalReport")
    @ResponseBody
    public String aicaiOwnGoalReport() throws Exception {
        aicaiService.aicaiOwnGoalReport();
        return "ok";
    }

    @RequestMapping("/callBack1")
    @ResponseBody
    public JSONObject callback1(@RequestBody String requestBody) throws Exception {
        JSONObject res = new JSONObject();
        log.info(requestBody);
        res.put("code", "200");
        return res;
    }

    @RequestMapping("/qx/repair")
    @ResponseBody
    public JSONObject
    repair(String encrypt) {
        log.info("encrypt:{}", encrypt);
        JSONObject jsonObject = JSONObject.parseObject(encrypt);
        String callbackUrl = jsonObject.getString("callback_url");
        log.info("callbackUrl:{}", callbackUrl);
        String s = FileUtil.readString("D:\\乱七八糟\\原型图\\回参\\圈讯车辆维修保养记录4.txt", Charset.defaultCharset());
        String body = HttpUtil.createPost(callbackUrl).body(s).execute().body();
        log.info("{}", body);
        JSONObject res = new JSONObject();

        JSONObject res2 = new JSONObject();
        res2.put("code", "300018");
        res.put("encrypt", res2);
        log.info(res2.toJSONString());
        return res;
    }
}
